package kevin.framework.basic.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import kevin.framework.basic.common.ui.AjaxResult;
import kevin.framework.basic.common.ui.ClientButton;
import kevin.framework.basic.common.ui.DataGrid;
import kevin.framework.basic.domain.Org;
import kevin.framework.basic.service.IBaseService;
import kevin.framework.basic.service.IOrgService;

/**
 * Generate by Kevin's CodeBuilder
 **/
@Controller
@RequestMapping(value = "/org")
public class OrgController extends ResetfulBaseController<Org, String> {

    IOrgService orgService;

    @Resource(name = "orgService")
    @Override
    public void setService(IBaseService<Org, String> service) {
        this.service = service;
        orgService = (IOrgService)service;
    }

    @Override
    @RequestMapping(value = "/page/{pageName}", method = RequestMethod.GET)
    public String page(@PathVariable("pageName") String pageName, HttpServletRequest request) throws Exception {
        if ("tree".equals(pageName)) {
            request.setAttribute("gridid", "org");
            AjaxResult json = this.query(request);
            request.setAttribute("treeJson", ((JSONArray)json.data).toJSONString());
        }
        return super.page(pageName, request);
    }

    /*
     * 页面 views/base/entityPath 文件夹路径
     * ***/
    @Override
    public String setPageModelPath(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return "base" + File.separator + entityPath;
    }

    @Override
    @RequestMapping(value = "/query")
    public @ResponseBody AjaxResult query(HttpServletRequest request) throws Exception {
        String pid = request.getParameter("pid");
        if (StringUtils.isEmpty(pid)) {
            pid = "0";
        }
        List<ClientButton> buttonlist;

        DataGrid grid = getDataGridCfg(request);
        if (grid != null) {
            buttonlist = grid.getLineButton();
        } else {
            buttonlist = new ArrayList<ClientButton>();
        }

        Map<String, Object> param = new HashMap<String, Object>();
        Map<String, String> order = new HashMap<String, String>();
        order.put("orderIndex", "asc");
        param.put("orderColumn", order);
        JSONArray array = orgService.getOrgJsonByPid(pid, buttonlist, param);
        return this.returnSuccData(array);
    }

    @Override
    public void buttonCreateHanlder(Org bean, ClientButton button) {
        if (bean != null) {
            if ("0".equals(bean.getPid())) {
                if ("lineAddFn".equals(button.getHandler())) {
                    button.setVisualable(false);
                }
                if (bean.getOrgType().equals(0) && "lineChildFn".equals(button.getHandler())) {
                    button.setVisualable(false);
                }
            } else if ("lineChildFn".equals(button.getHandler())) {
                button.setVisualable(false);
            }
        }
    }

    @Override
    protected Map<String, Object> setListParams(HttpServletRequest request) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String pid = request.getParameter("pid");
        params.put("pid", pid);
        return params;
    }

    // @Override
    // @RequestMapping(value = "/list")
    // public AjaxResult list(HttpServletRequest request) throws Exception {
    // return this.query(request);
    // }

}
